home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / Python 1.3 PPC / Lib / tkinter / Tkinter.py < prev   
Encoding:
Python Source  |  1995-10-11  |  43.4 KB  |  1,369 lines  |  [TEXT/PYTH]

  1. # Tkinter.py -- Tk/Tcl widget wrappers
  2.  
  3. import tkinter
  4. from tkinter import TclError
  5. from types import *
  6. from Tkconstants import *
  7.  
  8. CallableTypes = (FunctionType, MethodType,
  9.          BuiltinFunctionType, BuiltinMethodType)
  10.  
  11. TkVersion = eval(tkinter.TK_VERSION)
  12. TclVersion = eval(tkinter.TCL_VERSION)
  13.  
  14.  
  15. def _flatten(tuple):
  16.     res = ()
  17.     for item in tuple:
  18.         if type(item) in (TupleType, ListType):
  19.             res = res + _flatten(item)
  20.         elif item is not None:
  21.             res = res + (item,)
  22.     return res
  23.  
  24. def _cnfmerge(cnfs):
  25.     if type(cnfs) is DictionaryType:
  26.         _fixgeometry(cnfs)
  27.         return cnfs
  28.     elif type(cnfs) in (NoneType, StringType):
  29.         
  30.         return cnfs
  31.     else:
  32.         cnf = {}
  33.         for c in _flatten(cnfs):
  34.             _fixgeometry(c)
  35.             for k, v in c.items():
  36.                 cnf[k] = v
  37.         return cnf
  38.  
  39. if TkVersion >= 4.0:
  40.     _fixg_warning = "Warning: patched up pre-Tk-4.0 geometry option\n"
  41.     def _fixgeometry(c):
  42.         if c and c.has_key('geometry'):
  43.             wh = _parsegeometry(c['geometry'])
  44.             if wh:
  45.                 # Print warning message -- once
  46.                 global _fixg_warning
  47.                 if _fixg_warning:
  48.                     import sys
  49.                     sys.stderr.write(_fixg_warning)
  50.                     _fixg_warning = None
  51.                 w, h = wh
  52.                 c['width'] = w
  53.                 c['height'] = h
  54.                 del c['geometry']
  55.     def _parsegeometry(s):
  56.         from string import splitfields
  57.         fields = splitfields(s, 'x')
  58.         if len(fields) == 2:
  59.             return tuple(fields)
  60.         # else: return None
  61. else:
  62.     def _fixgeometry(c): pass
  63.  
  64. class Event:
  65.     pass
  66.  
  67. _default_root = None
  68.  
  69. def _tkerror(err):
  70.     pass
  71.  
  72. def _exit(code='0'):
  73.     import sys
  74.     sys.exit(getint(code))
  75.  
  76. _varnum = 0
  77. class Variable:
  78.     def __init__(self, master=None):
  79.         global _default_root
  80.         global _varnum
  81.         if master:
  82.             self._tk = master.tk
  83.         else:
  84.             self._tk = _default_root.tk
  85.         self._name = 'PY_VAR' + `_varnum`
  86.         _varnum = _varnum + 1
  87.     def __del__(self):
  88.         self._tk.unsetvar(self._name)
  89.     def __str__(self):
  90.         return self._name
  91.     def __call__(self, value=None):
  92.         if value == None:
  93.             return self.get()
  94.         else:
  95.             self.set(value)
  96.     def set(self, value):
  97.         return self._tk.setvar(self._name, value)
  98.  
  99. class StringVar(Variable):
  100.     def __init__(self, master=None):
  101.         Variable.__init__(self, master)
  102.     def get(self):
  103.         return self._tk.getvar(self._name)
  104.  
  105. class IntVar(Variable):
  106.     def __init__(self, master=None):
  107.         Variable.__init__(self, master)
  108.     def get(self):
  109.         return self._tk.getint(self._tk.getvar(self._name))
  110.  
  111. class DoubleVar(Variable):
  112.     def __init__(self, master=None):
  113.         Variable.__init__(self, master)
  114.     def get(self):
  115.         return self._tk.getdouble(self._tk.getvar(self._name))
  116.  
  117. class BooleanVar(Variable):
  118.     def __init__(self, master=None):
  119.         Variable.__init__(self, master)
  120.     def get(self):
  121.         return self._tk.getboolean(self._tk.getvar(self._name))
  122.  
  123. def mainloop(n=0):
  124.     _default_root.tk.mainloop(n)
  125.  
  126. def getint(s):
  127.     return _default_root.tk.getint(s)
  128.  
  129. def getdouble(s):
  130.     return _default_root.tk.getdouble(s)
  131.  
  132. def getboolean(s):
  133.     return _default_root.tk.getboolean(s)
  134.  
  135. class Misc:
  136.     def tk_strictMotif(self, boolean=None):
  137.         return self.tk.getboolean(self.tk.call(
  138.             'set', 'tk_strictMotif', boolean))
  139.     def tk_menuBar(self, *args):
  140.         apply(self.tk.call, ('tk_menuBar', self._w) + args)
  141.     def wait_variable(self, name='PY_VAR'):
  142.         self.tk.call('tkwait', 'variable', name)
  143.     waitvar = wait_variable # XXX b/w compat
  144.     def wait_window(self, window=None):
  145.         if window == None:
  146.             window = self
  147.         self.tk.call('tkwait', 'window', window._w)
  148.     def wait_visibility(self, window=None):
  149.         if window == None:
  150.             window = self
  151.         self.tk.call('tkwait', 'visibility', window._w)
  152.     def setvar(self, name='PY_VAR', value='1'):
  153.         self.tk.setvar(name, value)
  154.     def getvar(self, name='PY_VAR'):
  155.         return self.tk.getvar(name)
  156.     def getint(self, s):
  157.         return self.tk.getint(s)
  158.     def getdouble(self, s):
  159.         return self.tk.getdouble(s)
  160.     def getboolean(self, s):
  161.         return self.tk.getboolean(s)
  162.     def focus_set(self):
  163.         self.tk.call('focus', self._w)
  164.     focus = focus_set # XXX b/w compat?
  165.     def focus_default_set(self):
  166.         self.tk.call('focus', 'default', self._w)
  167.     def focus_default_none(self):
  168.         self.tk.call('focus', 'default', 'none')
  169.     focus_default = focus_default_set
  170.     def focus_none(self):
  171.         self.tk.call('focus', 'none')
  172.     def focus_get(self):
  173.         name = self.tk.call('focus')
  174.         if name == 'none': return None
  175.         return self._nametowidget(name)
  176.     def after(self, ms, func=None, *args):
  177.         if not func:
  178.             # I'd rather use time.sleep(ms*0.001)
  179.             self.tk.call('after', ms)
  180.         else:
  181.             # XXX Disgusting hack to clean up after calling func
  182.             tmp = []
  183.             def callit(func=func, args=args, tk=self.tk, tmp=tmp):
  184.                 try:
  185.                     apply(func, args)
  186.                 finally:
  187.                     tk.deletecommand(tmp[0])
  188.             name = self._register(callit)
  189.             tmp.append(name)
  190.             return self.tk.call('after', ms, name)
  191.     def after_idle(self, func, *args):
  192.         return apply(self.after, ('idle', func) + args)
  193.     def after_cancel(self, id):
  194.         self.tk.call('after', 'cancel', id)
  195.     def bell(self, displayof=None):
  196.         if displayof:
  197.             self.tk.call('bell', '-displayof', displayof)
  198.         else:
  199.             self.tk.call('bell', '-displayof', self._w)
  200.     # XXX grab current w/o window argument
  201.     def grab_current(self):
  202.         name = self.tk.call('grab', 'current', self._w)
  203.         if not name: return None
  204.         return self._nametowidget(name)
  205.     def grab_release(self):
  206.         self.tk.call('grab', 'release', self._w)
  207.     def grab_set(self):
  208.         self.tk.call('grab', 'set', self._w)
  209.     def grab_set_global(self):
  210.         self.tk.call('grab', 'set', '-global', self._w)
  211.     def grab_status(self):
  212.         status = self.tk.call('grab', 'status', self._w)
  213.         if status == 'none': status = None
  214.         return status
  215.     def lower(self, belowThis=None):
  216.         self.tk.call('lower', self._w, belowThis)
  217.     def option_add(self, pattern, value, priority = None):
  218.         self.tk.call('option', 'add', pattern, value, priority)
  219.     def option_clear(self):
  220.         self.tk.call('option', 'clear')
  221.     def option_get(self, name, className):
  222.         return self.tk.call('option', 'get', self._w, name, className)
  223.     def option_readfile(self, fileName, priority = None):
  224.         self.tk.call('option', 'readfile', fileName, priority)
  225.     def selection_clear(self):
  226.         self.tk.call('selection', 'clear', self._w)
  227.     def selection_get(self, type=None):
  228.         return self.tk.call('selection', 'get', type)
  229.     def selection_handle(self, func, type=None, format=None):
  230.         name = self._register(func)
  231.         self.tk.call('selection', 'handle', self._w, 
  232.                  name, type, format)
  233.     def selection_own(self, func=None):
  234.         name = self._register(func)
  235.         self.tk.call('selection', 'own', self._w, name)
  236.     def selection_own_get(self):
  237.         return self._nametowidget(self.tk.call('selection', 'own'))
  238.     def send(self, interp, cmd, *args):
  239.         return apply(self.tk.call, ('send', interp, cmd) + args)
  240.     def lower(self, belowThis=None):
  241.         self.tk.call('lift', self._w, belowThis)
  242.     def tkraise(self, aboveThis=None):
  243.         self.tk.call('raise', self._w, aboveThis)
  244.     lift = tkraise
  245.     def colormodel(self, value=None):
  246.         return self.tk.call('tk', 'colormodel', self._w, value)
  247.     def winfo_atom(self, name):
  248.         return self.tk.getint(self.tk.call('winfo', 'atom', name))
  249.     def winfo_atomname(self, id):
  250.         return self.tk.call('winfo', 'atomname', id)
  251.     def winfo_cells(self):
  252.         return self.tk.getint(
  253.             self.tk.call('winfo', 'cells', self._w))
  254.     def winfo_children(self):
  255.         return map(self._nametowidget,
  256.                self.tk.splitlist(self.tk.call(
  257.                    'winfo', 'children', self._w)))
  258.     def winfo_class(self):
  259.         return self.tk.call('winfo', 'class', self._w)
  260.     def winfo_containing(self, rootX, rootY):
  261.         return self.tk.call('winfo', 'containing', rootx, rootY)
  262.     def winfo_depth(self):
  263.         return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
  264.     def winfo_exists(self):
  265.         return self.tk.getint(
  266.             self.tk.call('winfo', 'exists', self._w))
  267.     def winfo_fpixels(self, number):
  268.         return self.tk.getdouble(self.tk.call(
  269.             'winfo', 'fpixels', self._w, number))
  270.     def winfo_geometry(self):
  271.         return self.tk.call('winfo', 'geometry', self._w)
  272.     def winfo_height(self):
  273.         return self.tk.getint(
  274.             self.tk.call('winfo', 'height', self._w))
  275.     def winfo_id(self):
  276.         return self.tk.getint(
  277.             self.tk.call('winfo', 'id', self._w))
  278.     def winfo_interps(self):
  279.         return self.tk.splitlist(
  280.             self.tk.call('winfo', 'interps'))
  281.     def winfo_ismapped(self):
  282.         return self.tk.getint(
  283.             self.tk.call('winfo', 'ismapped', self._w))
  284.     def winfo_name(self):
  285.         return self.tk.call('winfo', 'name', self._w)
  286.     def winfo_parent(self):
  287.         return self.tk.call('winfo', 'parent', self._w)
  288.     def winfo_pathname(self, id):
  289.         return self.tk.call('winfo', 'pathname', id)
  290.     def winfo_pixels(self, number):
  291.         return self.tk.getint(
  292.             self.tk.call('winfo', 'pixels', self._w, number))
  293.     def winfo_reqheight(self):
  294.         return self.tk.getint(
  295.             self.tk.call('winfo', 'reqheight', self._w))
  296.     def winfo_reqwidth(self):
  297.         return self.tk.getint(
  298.             self.tk.call('winfo', 'reqwidth', self._w))
  299.     def winfo_rgb(self, color):
  300.         return self._getints(
  301.             self.tk.call('winfo', 'rgb', self._w, color))
  302.     def winfo_rootx(self):
  303.         return self.tk.getint(
  304.             self.tk.call('winfo', 'rootx', self._w))
  305.     def winfo_rooty(self):
  306.         return self.tk.getint(
  307.             self.tk.call('winfo', 'rooty', self._w))
  308.     def winfo_screen(self):
  309.         return self.tk.call('winfo', 'screen', self._w)
  310.     def winfo_screencells(self):
  311.         return self.tk.getint(
  312.             self.tk.call('winfo', 'screencells', self._w))
  313.     def winfo_screendepth(self):
  314.         return self.tk.getint(
  315.             self.tk.call('winfo', 'screendepth', self._w))
  316.     def winfo_screenheight(self):
  317.         return self.tk.getint(
  318.             self.tk.call('winfo', 'screenheight', self._w))
  319.     def winfo_screenmmheight(self):
  320.         return self.tk.getint(
  321.             self.tk.call('winfo', 'screenmmheight', self._w))
  322.     def winfo_screenmmwidth(self):
  323.         return self.tk.getint(
  324.             self.tk.call('winfo', 'screenmmwidth', self._w))
  325.     def winfo_screenvisual(self):
  326.         return self.tk.call('winfo', 'screenvisual', self._w)
  327.     def winfo_screenwidth(self):
  328.         return self.tk.getint(
  329.             self.tk.call('winfo', 'screenwidth', self._w))
  330.     def winfo_toplevel(self):
  331.         return self._nametowidget(self.tk.call(
  332.             'winfo', 'toplevel', self._w))
  333.     def winfo_visual(self):
  334.         return self.tk.call('winfo', 'visual', self._w)
  335.     def winfo_vrootheight(self):
  336.         return self.tk.getint(
  337.             self.tk.call('winfo', 'vrootheight', self._w))
  338.     def winfo_vrootwidth(self):
  339.         return self.tk.getint(
  340.             self.tk.call('winfo', 'vrootwidth', self._w))
  341.     def winfo_vrootx(self):
  342.         return self.tk.getint(
  343.             self.tk.call('winfo', 'vrootx', self._w))
  344.     def winfo_vrooty(self):
  345.         return self.tk.getint(
  346.             self.tk.call('winfo', 'vrooty', self._w))
  347.     def winfo_width(self):
  348.         return self.tk.getint(
  349.             self.tk.call('winfo', 'width', self._w))
  350.     def winfo_x(self):
  351.         return self.tk.getint(
  352.             self.tk.call('winfo', 'x', self._w))
  353.     def winfo_y(self):
  354.         return self.tk.getint(
  355.             self.tk.call('winfo', 'y', self._w))
  356.     def update(self):
  357.         self.tk.call('update')
  358.     def update_idletasks(self):
  359.         self.tk.call('update', 'idletasks')
  360.     def bind(self, sequence, func=None, add=''):
  361.         if add: add = '+'
  362.         if func:
  363.             name = self._register(func, self._substitute)
  364.             self.tk.call('bind', self._w, sequence, 
  365.                  (add + name,) + self._subst_format)
  366.         else:
  367.             return self.tk.call('bind', self._w, sequence)
  368.     def unbind(self, sequence):
  369.         self.tk.call('bind', self._w, sequence, '')
  370.     def bind_all(self, sequence, func=None, add=''):
  371.         if add: add = '+'
  372.         if func:
  373.             name = self._register(func, self._substitute)
  374.             self.tk.call('bind', 'all' , sequence, 
  375.                  (add + name,) + self._subst_format)
  376.         else:
  377.             return self.tk.call('bind', 'all', sequence)
  378.     def unbind_all(self, sequence):
  379.         self.tk.call('bind', 'all' , sequence, '')
  380.     def bind_class(self, className, sequence, func=None, add=''):
  381.         if add: add = '+'
  382.         if func:
  383.             name = self._register(func, self._substitute)
  384.             self.tk.call('bind', className , sequence, 
  385.                  (add + name,) + self._subst_format)
  386.         else:
  387.             return self.tk.call('bind', className, sequence)
  388.     def unbind_class(self, className, sequence):
  389.         self.tk.call('bind', className , sequence, '')
  390.     def mainloop(self, n=0):
  391.         self.tk.mainloop(n)
  392.     def quit(self):
  393.         self.tk.quit()
  394.     def _getints(self, string):
  395.         if not string: return None
  396.         return tuple(map(self.tk.getint, self.tk.splitlist(string)))
  397.     def _getdoubles(self, string):
  398.         if not string: return None
  399.         return tuple(map(self.tk.getdouble, self.tk.splitlist(string)))
  400.     def _getboolean(self, string):
  401.         if string:
  402.             return self.tk.getboolean(string)
  403.     def _options(self, cnf, kw = None):
  404.         if kw:
  405.             cnf = _cnfmerge((cnf, kw))
  406.         else:
  407.             cnf = _cnfmerge(cnf)
  408.         res = ()
  409.         for k, v in cnf.items():
  410.             if k[-1] == '_': k = k[:-1]
  411.             if type(v) in CallableTypes:
  412.                 v = self._register(v)
  413.             res = res + ('-'+k, v)
  414.         return res
  415.     def _nametowidget(self, name):
  416.         w = self
  417.         if name[0] == '.':
  418.             w = w._root()
  419.             name = name[1:]
  420.         from string import find
  421.         while name:
  422.             i = find(name, '.')
  423.             if i >= 0:
  424.                 name, tail = name[:i], name[i+1:]
  425.             else:
  426.                 tail = ''
  427.             w = w.children[name]
  428.             name = tail
  429.         return w
  430.     def _register(self, func, subst=None):
  431.         f = self._wrap(func, subst)
  432.         name = `id(f)`
  433.         if hasattr(func, 'im_func'):
  434.             func = func.im_func
  435.         if hasattr(func, '__name__') and \
  436.            type(func.__name__) == type(''):
  437.             name = name + func.__name__
  438.         self.tk.createcommand(name, f)
  439.         return name
  440.     register = _register
  441.     def _root(self):
  442.         w = self
  443.         while w.master: w = w.master
  444.         return w
  445.     _subst_format = ('%#', '%b', '%f', '%h', '%k', 
  446.              '%s', '%t', '%w', '%x', '%y',
  447.              '%A', '%E', '%K', '%N', '%W', '%T', '%X', '%Y')
  448.     def _substitute(self, *args):
  449.         tk = self.tk
  450.         if len(args) != len(self._subst_format): return args
  451.         nsign, b, f, h, k, s, t, w, x, y, A, E, K, N, W, T, X, Y = args
  452.         # Missing: (a, c, d, m, o, v, B, R)
  453.         e = Event()
  454.         e.serial = tk.getint(nsign)
  455.         e.num = tk.getint(b)
  456.         try: e.focus = tk.getboolean(f)
  457.         except TclError: pass
  458.         e.height = tk.getint(h)
  459.         e.keycode = tk.getint(k)
  460.         e.state = tk.getint(s)
  461.         e.time = tk.getint(t)
  462.         e.width = tk.getint(w)
  463.         e.x = tk.getint(x)
  464.         e.y = tk.getint(y)
  465.         e.char = A
  466.         try: e.send_event = tk.getboolean(E)
  467.         except TclError: pass
  468.         e.keysym = K
  469.         e.keysym_num = tk.getint(N)
  470.         e.type = T
  471.         e.widget = self._nametowidget(W)
  472.         e.x_root = tk.getint(X)
  473.         e.y_root = tk.getint(Y)
  474.         return (e,)
  475.     def _report_exception(self):
  476.         import sys
  477.         exc, val, tb = sys.exc_type, sys.exc_value, sys.exc_traceback
  478.         root = self._root()
  479.         root.report_callback_exception(exc, val, tb)
  480.     def _wrap(self, func, subst=None):
  481.         return CallWrapper(func, subst, self).__call__
  482.  
  483. class CallWrapper:
  484.     def __init__(self, func, subst, widget):
  485.         self.func = func
  486.         self.subst = subst
  487.         self.widget = widget
  488.     def __call__(self, *args):
  489.         try:
  490.             if self.subst:
  491.                 args = apply(self.subst, args)
  492.             return apply(self.func, args)
  493.         except SystemExit, msg:
  494.             raise SystemExit, msg
  495.         except:
  496.             self.widget._report_exception()
  497.  
  498. class Wm:
  499.     def aspect(self, 
  500.            minNumer=None, minDenom=None, 
  501.            maxNumer=None, maxDenom=None):
  502.         return self._getints(
  503.             self.tk.call('wm', 'aspect', self._w, 
  504.                      minNumer, minDenom, 
  505.                      maxNumer, maxDenom))
  506.     def client(self, name=None):
  507.         return self.tk.call('wm', 'client', self._w, name)
  508.     def command(self, value=None):
  509.         return self.tk.call('wm', 'command', self._w, value)
  510.     def deiconify(self):
  511.         return self.tk.call('wm', 'deiconify', self._w)
  512.     def focusmodel(self, model=None):
  513.         return self.tk.call('wm', 'focusmodel', self._w, model)
  514.     def frame(self):
  515.         return self.tk.call('wm', 'frame', self._w)
  516.     def geometry(self, newGeometry=None):
  517.         return self.tk.call('wm', 'geometry', self._w, newGeometry)
  518.     def grid(self,
  519.          baseWidht=None, baseHeight=None, 
  520.          widthInc=None, heightInc=None):
  521.         return self._getints(self.tk.call(
  522.             'wm', 'grid', self._w,
  523.             baseWidht, baseHeight, widthInc, heightInc))
  524.     def group(self, pathName=None):
  525.         return self.tk.call('wm', 'group', self._w, pathName)
  526.     def iconbitmap(self, bitmap=None):
  527.         return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
  528.     def iconify(self):
  529.         return self.tk.call('wm', 'iconify', self._w)
  530.     def iconmask(self, bitmap=None):
  531.         return self.tk.call('wm', 'iconmask', self._w, bitmap)
  532.     def iconname(self, newName=None):
  533.         return self.tk.call('wm', 'iconname', self._w, newName)
  534.     def iconposition(self, x=None, y=None):
  535.         return self._getints(self.tk.call(
  536.             'wm', 'iconposition', self._w, x, y))
  537.     def iconwindow(self, pathName=None):
  538.         return self.tk.call('wm', 'iconwindow', self._w, pathName)
  539.     def maxsize(self, width=None, height=None):
  540.         return self._getints(self.tk.call(
  541.             'wm', 'maxsize', self._w, width, height))
  542.     def minsize(self, width=None, height=None):
  543.         return self._getints(self.tk.call(
  544.             'wm', 'minsize', self._w, width, height))
  545.     def overrideredirect(self, boolean=None):
  546.         return self._getboolean(self.tk.call(
  547.             'wm', 'overrideredirect', self._w, boolean))
  548.     def positionfrom(self, who=None):
  549.         return self.tk.call('wm', 'positionfrom', self._w, who)
  550.     def protocol(self, name=None, func=None):
  551.         if type(func) in CallableTypes:
  552.             command = self._register(func)
  553.         else:
  554.             command = func
  555.         return self.tk.call(
  556.             'wm', 'protocol', self._w, name, command)
  557.     def sizefrom(self, who=None):
  558.         return self.tk.call('wm', 'sizefrom', self._w, who)
  559.     def state(self):
  560.         return self.tk.call('wm', 'state', self._w)
  561.     def title(self, string=None):
  562.         return self.tk.call('wm', 'title', self._w, string)
  563.     def transient(self, master=None):
  564.         return self.tk.call('wm', 'transient', self._w, master)
  565.     def withdraw(self):
  566.         return self.tk.call('wm', 'withdraw', self._w)
  567.  
  568. class Tk(Misc, Wm):
  569.     _w = '.'
  570.     def __init__(self, screenName=None, baseName=None, className='Tk'):
  571.         global _default_root
  572.         self.master = None
  573.         self.children = {}
  574.         if baseName is None:
  575.             import sys, os
  576.             baseName = os.path.basename(sys.argv[0])
  577.             if baseName[-3:] == '.py': baseName = baseName[:-3]
  578.         self.tk = tkinter.create(screenName, baseName, className)
  579.         # Version sanity checks
  580.         tk_version = self.tk.getvar('tk_version')
  581.         if tk_version != tkinter.TK_VERSION:
  582.             raise RuntimeError, \
  583.             "tk.h version (%s) doesn't match libtk.a version (%s)" \
  584.             % (tkinter.TK_VERSION, tk_version)
  585.         tcl_version = self.tk.getvar('tcl_version')
  586.         if tcl_version != tkinter.TCL_VERSION:
  587.             raise RuntimeError, \
  588.             "tcl.h version (%s) doesn't match libtcl.a version (%s)" \
  589.             % (tkinter.TCL_VERSION, tcl_version)
  590.         if TkVersion < 4.0:
  591.             raise RuntimeError, \
  592.               "Tk 4.0 or higher is required; found Tk %s" \
  593.               % str(TkVersion)
  594.         self.tk.createcommand('tkerror', _tkerror)
  595.         self.tk.createcommand('exit', _exit)
  596.         self.readprofile(baseName, className)
  597.         if not _default_root:
  598.             _default_root = self
  599.     def destroy(self):
  600.         for c in self.children.values(): c.destroy()
  601.         self.tk.call('destroy', self._w)
  602.     def __str__(self):
  603.         return self._w
  604.     def readprofile(self, baseName, className):
  605.         ##print __import__
  606.         import os, pdb
  607.         if os.environ.has_key('HOME'): home = os.environ['HOME']
  608.         else: home = os.curdir
  609.         class_tcl = os.path.join(home, '.%s.tcl' % className)
  610.         class_py = os.path.join(home, '.%s.py' % className)
  611.         base_tcl = os.path.join(home, '.%s.tcl' % baseName)
  612.         base_py = os.path.join(home, '.%s.py' % baseName)
  613.         dir = {'self': self}
  614.         ##pdb.run('from Tkinter import *', dir)
  615.         exec 'from Tkinter import *' in dir
  616.         if os.path.isfile(class_tcl):
  617.             print 'source', `class_tcl`
  618.             self.tk.call('source', class_tcl)
  619.         if os.path.isfile(class_py):
  620.             print 'execfile', `class_py`
  621.             execfile(class_py, dir)
  622.         if os.path.isfile(base_tcl):
  623.             print 'source', `base_tcl`
  624.             self.tk.call('source', base_tcl)
  625.         if os.path.isfile(base_py):
  626.             print 'execfile', `base_py`
  627.             execfile(base_py, dir)
  628.     def report_callback_exception(self, exc, val, tb):
  629.         import traceback
  630.         print "Exception in Tkinter callback"
  631.         traceback.print_exception(exc, val, tb)
  632.  
  633. class Pack:
  634.     def config(self, cnf={}, **kw):
  635.         apply(self.tk.call, 
  636.               ('pack', 'configure', self._w) 
  637.               + self._options(cnf, kw))
  638.     pack = config
  639.     def __setitem__(self, key, value):
  640.         Pack.config({key: value})
  641.     def forget(self):
  642.         self.tk.call('pack', 'forget', self._w)
  643.     def newinfo(self):
  644.         words = self.tk.splitlist(
  645.             self.tk.call('pack', 'newinfo', self._w))
  646.         dict = {}
  647.         for i in range(0, len(words), 2):
  648.             key = words[i][1:]
  649.             value = words[i+1]
  650.             if value[0] == '.':
  651.                 value = self._nametowidget(value)
  652.             dict[key] = value
  653.         return dict
  654.     info = newinfo
  655.     _noarg_ = ['_noarg_']
  656.     def propagate(self, flag=_noarg_):
  657.         if flag is Pack._noarg_:
  658.             return self._getboolean(self.tk.call(
  659.                 'pack', 'propagate', self._w))
  660.         else:
  661.             self.tk.call('pack', 'propagate', self._w, flag)
  662.     def slaves(self):
  663.         return map(self._nametowidget,
  664.                self.tk.splitlist(
  665.                    self.tk.call('pack', 'slaves', self._w)))
  666.  
  667. class Place:
  668.     def config(self, cnf={}, **kw):
  669.         apply(self.tk.call, 
  670.               ('place', 'configure', self._w) 
  671.               + self._options(cnf, kw))
  672.     place = config
  673.     def __setitem__(self, key, value):
  674.         Place.config({key: value})
  675.     def forget(self):
  676.         self.tk.call('place', 'forget', self._w)
  677.     def info(self):
  678.         return self.tk.call('place', 'info', self._w)
  679.     def slaves(self):
  680.         return map(self._nametowidget,
  681.                self.tk.splitlist(
  682.                    self.tk.call(
  683.                        'place', 'slaves', self._w)))
  684.  
  685. class Widget(Misc, Pack, Place):
  686.     def _setup(self, master, cnf):
  687.         global _default_root
  688.         if not master:
  689.             if not _default_root:
  690.                 _default_root = Tk()
  691.             master = _default_root
  692.         if not _default_root:
  693.             _default_root = master
  694.         self.master = master
  695.         self.tk = master.tk
  696.         if cnf.has_key('name'):
  697.             name = cnf['name']
  698.             del cnf['name']
  699.         else:
  700.             name = `id(self)`
  701.         self._name = name
  702.         if master._w=='.':
  703.             self._w = '.' + name
  704.         else:
  705.             self._w = master._w + '.' + name
  706.         self.children = {}
  707.         if self.master.children.has_key(self._name):
  708.             self.master.children[self._name].destroy()
  709.         self.master.children[self._name] = self
  710.     def __init__(self, master, widgetName, cnf={}, kw={}, extra=()):
  711.         if kw:
  712.             cnf = _cnfmerge((cnf, kw))
  713.         self.widgetName = widgetName
  714.         Widget._setup(self, master, cnf)
  715.         apply(self.tk.call, (widgetName, self._w)+extra)
  716.         if cnf:
  717.             Widget.config(self, cnf)
  718.     def config(self, cnf=None, **kw):
  719.         # XXX ought to generalize this so tag_config etc. can use it
  720.         if kw:
  721.             cnf = _cnfmerge((cnf, kw))
  722.         else:
  723.             cnf = _cnfmerge(cnf)
  724.         if cnf is None:
  725.             cnf = {}
  726.             for x in self.tk.split(
  727.                 self.tk.call(self._w, 'configure')):
  728.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  729.             return cnf
  730.         if type(cnf) == StringType:
  731.             x = self.tk.split(self.tk.call(
  732.                 self._w, 'configure', '-'+cnf))
  733.             return (x[0][1:],) + x[1:]
  734.         for k in cnf.keys():
  735.             if type(k) == ClassType:
  736.                 k.config(self, cnf[k])
  737.                 del cnf[k]
  738.         apply(self.tk.call, (self._w, 'configure')
  739.               + self._options(cnf))
  740.     def __getitem__(self, key):
  741.         if TkVersion >= 4.0:
  742.             return self.tk.call(self._w, 'cget', '-' + key)
  743.         v = self.tk.splitlist(self.tk.call(
  744.             self._w, 'configure', '-' + key))
  745.         return v[4]
  746.     def __setitem__(self, key, value):
  747.         Widget.config(self, {key: value})
  748.     def keys(self):
  749.         return map(lambda x: x[0][1:],
  750.                self.tk.split(self.tk.call(self._w, 'configure')))
  751.     def __str__(self):
  752.         return self._w
  753.     def destroy(self):
  754.         for c in self.children.values(): c.destroy()
  755.         if self.master.children.has_key(self._name):
  756.             del self.master.children[self._name]
  757.         self.tk.call('destroy', self._w)
  758.     def _do(self, name, args=()):
  759.         return apply(self.tk.call, (self._w, name) + args)
  760.     # XXX The following method seems out of place here
  761. ##    def unbind_class(self, seq):
  762. ##        Misc.unbind_class(self, self.widgetName, seq)
  763.  
  764. class Toplevel(Widget, Wm):
  765.     def __init__(self, master=None, cnf={}, **kw):
  766.         if kw:
  767.             cnf = _cnfmerge((cnf, kw))
  768.         extra = ()
  769.         if cnf.has_key('screen'):
  770.             extra = ('-screen', cnf['screen'])
  771.             del cnf['screen']
  772.         if cnf.has_key('class'):
  773.             extra = extra + ('-class', cnf['class'])
  774.             del cnf['class']
  775.         Widget.__init__(self, master, 'toplevel', cnf, {}, extra)
  776.         root = self._root()
  777.         self.iconname(root.iconname())
  778.         self.title(root.title())
  779.  
  780. class Button(Widget):
  781.     def __init__(self, master=None, cnf={}, **kw):
  782.         Widget.__init__(self, master, 'button', cnf, kw)
  783.     def tkButtonEnter(self, *dummy):
  784.         self.tk.call('tkButtonEnter', self._w)
  785.     def tkButtonLeave(self, *dummy):
  786.         self.tk.call('tkButtonLeave', self._w)
  787.     def tkButtonDown(self, *dummy):
  788.         self.tk.call('tkButtonDown', self._w)
  789.     def tkButtonUp(self, *dummy):
  790.         self.tk.call('tkButtonUp', self._w)
  791.     def flash(self):
  792.         self.tk.call(self._w, 'flash')
  793.     def invoke(self):
  794.         self.tk.call(self._w, 'invoke')
  795.  
  796. # Indices:
  797. # XXX I don't like these -- take them away
  798. def AtEnd():
  799.     return 'end'
  800. def AtInsert(*args):
  801.     s = 'insert'
  802.     for a in args:
  803.         if a: s = s + (' ' + a)
  804.     return s
  805. def AtSelFirst():
  806.     return 'sel.first'
  807. def AtSelLast():
  808.     return 'sel.last'
  809. def At(x, y=None):
  810.     if y is None:
  811.         return '@' + `x`        
  812.     else:
  813.         return '@' + `x` + ',' + `y`
  814.  
  815. class Canvas(Widget):
  816.     def __init__(self, master=None, cnf={}, **kw):
  817.         Widget.__init__(self, master, 'canvas', cnf, kw)
  818.     def addtag(self, *args):
  819.         self._do('addtag', args)
  820.     def addtag_above(self, tagOrId):
  821.         self.addtag('above', tagOrId)
  822.     def addtag_all(self):
  823.         self.addtag('all')
  824.     def addtag_below(self, tagOrId):
  825.         self.addtag('below', tagOrId)
  826.     def addtag_closest(self, x, y, halo=None, start=None):
  827.         self.addtag('closest', x, y, halo, start)
  828.     def addtag_enclosed(self, x1, y1, x2, y2):
  829.         self.addtag('enclosed', x1, y1, x2, y2)
  830.     def addtag_overlapping(self, x1, y1, x2, y2):
  831.         self.addtag('overlapping', x1, y1, x2, y2)
  832.     def addtag_withtag(self, tagOrId):
  833.         self.addtag('withtag', tagOrId)
  834.     def bbox(self, *args):
  835.         return self._getints(self._do('bbox', args)) or None
  836.     def tag_unbind(self, tagOrId, sequence):
  837.         self.tk.call(self._w, 'bind', tagOrId, sequence, '')
  838.     def tag_bind(self, tagOrId, sequence, func, add=''):
  839.         if add: add='+'
  840.         name = self._register(func, self._substitute)
  841.         self.tk.call(self._w, 'bind', tagOrId, sequence, 
  842.                  (add + name,) + self._subst_format)
  843.     def canvasx(self, screenx, gridspacing=None):
  844.         return self.tk.getdouble(self.tk.call(
  845.             self._w, 'canvasx', screenx, gridspacing))
  846.     def canvasy(self, screeny, gridspacing=None):
  847.         return self.tk.getdouble(self.tk.call(
  848.             self._w, 'canvasy', screeny, gridspacing))
  849.     def coords(self, *args):
  850.         return self._do('coords', args)
  851.     def _create(self, itemType, args, kw): # Args: (value, value, ..., cnf={})
  852.         args = _flatten(args)
  853.         cnf = args[-1]
  854.         if type(cnf) in (DictionaryType, TupleType):
  855.             args = args[:-1]
  856.         else:
  857.             cnf = {}
  858.         return self.tk.getint(apply(
  859.             self.tk.call,
  860.             (self._w, 'create', itemType) 
  861.             + args + self._options(cnf, kw)))
  862.     def create_arc(self, *args, **kw):
  863.         return self._create('arc', args, kw)
  864.     def create_bitmap(self, *args, **kw):
  865.         return self._create('bitmap', args, kw)
  866.     def create_image(self, *args, **kw):
  867.         return self._create('image', args, kw)
  868.     def create_line(self, *args, **kw):
  869.         return self._create('line', args, kw)
  870.     def create_oval(self, *args, **kw):
  871.         return self._create('oval', args, kw)
  872.     def create_polygon(self, *args, **kw):
  873.         return self._create('polygon', args, kw)
  874.     def create_rectangle(self, *args, **kw):
  875.         return self._create('rectangle', args, kw)
  876.     def create_text(self, *args, **kw):
  877.         return self._create('text', args, kw)
  878.     def create_window(self, *args, **kw):
  879.         return self._create('window', args, kw)
  880.     def dchars(self, *args):
  881.         self._do('dchars', args)
  882.     def delete(self, *args):
  883.         self._do('delete', args)
  884.     def dtag(self, *args):
  885.         self._do('dtag', args)
  886.     def find(self, *args):
  887.         return self._getints(self._do('find', args))
  888.     def find_above(self, tagOrId):
  889.         return self.find('above', tagOrId)
  890.     def find_all(self):
  891.         return self.find('all')
  892.     def find_below(self, tagOrId):
  893.         return self.find('below', tagOrId)
  894.     def find_closest(self, x, y, halo=None, start=None):
  895.         return self.find('closest', x, y, halo, start)
  896.     def find_enclosed(self, x1, y1, x2, y2):
  897.         return self.find('enclosed', x1, y1, x2, y2)
  898.     def find_overlapping(self, x1, y1, x2, y2):
  899.         return self.find('overlapping', x1, y1, x2, y2)
  900.     def find_withtag(self, tagOrId):
  901.         return self.find('withtag', tagOrId)
  902.     def focus(self, *args):
  903.         return self._do('focus', args)
  904.     def gettags(self, *args):
  905.         return self.tk.splitlist(self._do('gettags', args))
  906.     def icursor(self, *args):
  907.         self._do('icursor', args)
  908.     def index(self, *args):
  909.         return self.tk.getint(self._do('index', args))
  910.     def insert(self, *args):
  911.         self._do('insert', args)
  912.     def itemconfig(self, tagOrId, cnf=None, **kw):
  913.         if cnf is None and not kw:
  914.             cnf = {}
  915.             for x in self.tk.split(
  916.                 self._do('itemconfigure', (tagOrId))):
  917.                 cnf[x[0][1:]] = (x[0][1:],) + x[1:]
  918.             return cnf
  919.         if type(cnf) == StringType and not kw:
  920.             x = self.tk.split(self._do('itemconfigure',
  921.                            (tagOrId, '-'+cnf,)))
  922.             return (x[0][1:],) + x[1:]
  923.         self._do('itemconfigure', (tagOrId,)
  924.              + self._options(cnf, kw))
  925.     def lower(self, *args):
  926.         self._do('lower', args)
  927.     def move(self, *args):
  928.         self._do('move', args)
  929.     def postscript(self, cnf={}, **kw):
  930.         return self._do('postscript', self._options(cnf, kw))
  931.     def tkraise(self, *args):
  932.         self._do('raise', args)
  933.     lift = tkraise
  934.     def scale(self, *args):
  935.         self._do('scale', args)
  936.     def scan_mark(self, x, y):
  937.         self.tk.call(self._w, 'scan', 'mark', x, y)
  938.     def scan_dragto(self, x, y):
  939.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  940.     def select_adjust(self, tagOrId, index):
  941.         self.tk.call(self._w, 'select', 'adjust', tagOrId, index)
  942.     def select_clear(self):
  943.         self.tk.call(self._w, 'select', 'clear', 'end')
  944.     def select_from(self, tagOrId, index):
  945.         self.tk.call(self._w, 'select', 'set', tagOrId, index)
  946.     def select_item(self):
  947.         self.tk.call(self._w, 'select', 'item')
  948.     def select_to(self, tagOrId, index):
  949.         self.tk.call(self._w, 'select', 'to', tagOrId, index)
  950.     def type(self, tagOrId):
  951.         return self.tk.call(self._w, 'type', tagOrId) or None
  952.     def xview(self, *args):
  953.         apply(self.tk.call, (self._w, 'xview')+args)
  954.     def yview(self, *args):
  955.         apply(self.tk.call, (self._w, 'yview')+args)
  956.  
  957. class Checkbutton(Widget):
  958.     def __init__(self, master=None, cnf={}, **kw):
  959.         Widget.__init__(self, master, 'checkbutton', cnf, kw)
  960.     def deselect(self):
  961.         self.tk.call(self._w, 'deselect')
  962.     def flash(self):
  963.         self.tk.call(self._w, 'flash')
  964.     def invoke(self):
  965.         self.tk.call(self._w, 'invoke')
  966.     def select(self):
  967.         self.tk.call(self._w, 'select')
  968.     def toggle(self):
  969.         self.tk.call(self._w, 'toggle')
  970.  
  971. class Entry(Widget):
  972.     def __init__(self, master=None, cnf={}, **kw):
  973.         Widget.__init__(self, master, 'entry', cnf, kw)
  974.     def tk_entryBackspace(self):
  975.         self.tk.call('tk_entryBackspace', self._w)
  976.     def tk_entryBackword(self):
  977.         self.tk.call('tk_entryBackword', self._w)
  978.     def tk_entrySeeCaret(self):
  979.         self.tk.call('tk_entrySeeCaret', self._w)
  980.     def delete(self, first, last=None):
  981.         self.tk.call(self._w, 'delete', first, last)
  982.     def get(self):
  983.         return self.tk.call(self._w, 'get')
  984.     def icursor(self, index):
  985.         self.tk.call(self._w, 'icursor', index)
  986.     def index(self, index):
  987.         return self.tk.getint(self.tk.call(
  988.             self._w, 'index', index))
  989.     def insert(self, index, string):
  990.         self.tk.call(self._w, 'insert', index, string)
  991.     def scan_mark(self, x):
  992.         self.tk.call(self._w, 'scan', 'mark', x)
  993.     def scan_dragto(self, x):
  994.         self.tk.call(self._w, 'scan', 'dragto', x)
  995.     def select_adjust(self, index):
  996.         self.tk.call(self._w, 'select', 'adjust', index)
  997.     def select_clear(self):
  998.         self.tk.call(self._w, 'select', 'clear', 'end')
  999.     def select_from(self, index):
  1000.             self.tk.call(self._w, 'select', 'set', index)
  1001.     def select_present(self):
  1002.         return self.tk.getboolean(
  1003.             self.tk.call(self._w, 'select', 'present'))
  1004.     def select_range(self, start, end):
  1005.         self.tk.call(self._w, 'select', 'range', start, end)
  1006.     def select_to(self, index):
  1007.         self.tk.call(self._w, 'select', 'to', index)
  1008.     def view(self, index):
  1009.         self.tk.call(self._w, 'view', index)
  1010.  
  1011. class Frame(Widget):
  1012.     def __init__(self, master=None, cnf={}, **kw):
  1013.         cnf = _cnfmerge((cnf, kw))
  1014.         extra = ()
  1015.         if cnf.has_key('class'):
  1016.             extra = ('-class', cnf['class'])
  1017.             del cnf['class']
  1018.         Widget.__init__(self, master, 'frame', cnf, {}, extra)
  1019.     def tk_menuBar(self, *args):
  1020.         apply(self.tk.call, ('tk_menuBar', self._w) + args)
  1021.  
  1022. class Label(Widget):
  1023.     def __init__(self, master=None, cnf={}, **kw):
  1024.         Widget.__init__(self, master, 'label', cnf, kw)
  1025.  
  1026. class Listbox(Widget):
  1027.     def __init__(self, master=None, cnf={}, **kw):
  1028.         Widget.__init__(self, master, 'listbox', cnf, kw)
  1029.     def tk_listboxSingleSelect(self):
  1030.         if TkVersion >= 4.0:
  1031.             self['selectmode'] = 'single'
  1032.         else:
  1033.             self.tk.call('tk_listboxSingleSelect', self._w) 
  1034.     def activate(self, index):
  1035.         self.tk.call(self._w, 'activate', index)
  1036.     def curselection(self):
  1037.         return self.tk.splitlist(self.tk.call(
  1038.             self._w, 'curselection'))
  1039.     def delete(self, first, last=None):
  1040.         self.tk.call(self._w, 'delete', first, last)
  1041.     def get(self, index):
  1042.         return self.tk.call(self._w, 'get', index)
  1043.     def insert(self, index, *elements):
  1044.         apply(self.tk.call,
  1045.               (self._w, 'insert', index) + elements)
  1046.     def nearest(self, y):
  1047.         return self.tk.getint(self.tk.call(
  1048.             self._w, 'nearest', y))
  1049.     def scan_mark(self, x, y):
  1050.         self.tk.call(self._w, 'scan', 'mark', x, y)
  1051.     def scan_dragto(self, x, y):
  1052.         self.tk.call(self._w, 'scan', 'dragto', x, y)
  1053.     def select_adjust(self, index):
  1054.         self.tk.call(self._w, 'select', 'adjust', index)
  1055.     if TkVersion >= 4.0:
  1056.         def select_anchor(self, index):
  1057.             self.tk.call(self._w, 'selection', 'anchor', index)
  1058.         def select_clear(self, first, last=None):
  1059.             self.tk.call(self._w,
  1060.                      'selection', 'clear', first, last)
  1061.         def select_includes(self, index):
  1062.             return self.tk.getboolean(self.tk.call(
  1063.                 self._w, 'selection', 'includes', index))
  1064.         def select_set(self, first, last=None):
  1065.             self.tk.call(self._w, 'selection', 'set', first, last)
  1066.     else:
  1067.         def select_clear(self):
  1068.             self.tk.call(self._w, 'select', 'clear')
  1069.         def select_from(self, index):
  1070.             self.tk.call(self._w, 'select', 'from', index)
  1071.         def select_to(self, index):
  1072.             self.tk.call(self._w, 'select', 'to', index)
  1073.     def size(self):
  1074.         return self.tk.getint(self.tk.call(self._w, 'size'))
  1075.     def xview(self, *what):
  1076.         apply(self.tk.call, (self._w, 'xview')+what)
  1077.     def yview(self, *what):
  1078.         apply(self.tk.call, (self._w, 'yview')+what)
  1079.  
  1080. class Menu(Widget):
  1081.     def __init__(self, master=None, cnf={}, **kw):
  1082.         Widget.__init__(self, master, 'menu', cnf, kw)
  1083.     def tk_bindForTraversal(self):
  1084.         self.tk.call('tk_bindForTraversal', self._w)
  1085.     def tk_mbPost(self):
  1086.         self.tk.call('tk_mbPost', self._w)
  1087.     def tk_mbUnpost(self):
  1088.         self.tk.call('tk_mbUnpost')
  1089.     def tk_traverseToMenu(self, char):
  1090.         self.tk.call('tk_traverseToMenu', self._w, char)
  1091.     def tk_traverseWithinMenu(self, char):
  1092.         self.tk.call('tk_traverseWithinMenu', self._w, char)
  1093.     def tk_getMenuButtons(self):
  1094.         return self.tk.call('tk_getMenuButtons', self._w)
  1095.     def tk_nextMenu(self, count):
  1096.         self.tk.call('tk_nextMenu', count)
  1097.     def tk_nextMenuEntry(self, count):
  1098.         self.tk.call('tk_nextMenuEntry', count)
  1099.     def tk_invokeMenu(self):
  1100.         self.tk.call('tk_invokeMenu', self._w)
  1101.     def tk_firstMenu(self):
  1102.         self.tk.call('tk_firstMenu', self._w)
  1103.     def tk_mbButtonDown(self):
  1104.         self.tk.call('tk_mbButtonDown', self._w)
  1105.     def activate(self, index):
  1106.         self.tk.call(self._w, 'activate', index)
  1107.     def add(self, itemType, cnf={}, **kw):
  1108.         apply(self.tk.call, (self._w, 'add', itemType) 
  1109.               + self._options(cnf, kw))
  1110.     def add_cascade(self, cnf={}, **kw):
  1111.         self.add('cascade', cnf or kw)
  1112.     def add_checkbutton(self, cnf={}, **kw):
  1113.         self.add('checkbutton', cnf or kw)
  1114.     def add_command(self, cnf={}, **kw):
  1115.         self.add('command', cnf or kw)
  1116.     def add_radiobutton(self, cnf={}, **kw):
  1117.         self.add('radiobutton', cnf or kw)
  1118.     def add_separator(self, cnf={}, **kw):
  1119.         self.add('separator', cnf or kw)
  1120.     def delete(self, index1, index2=None):
  1121.         self.tk.call(self._w, 'delete', index1, index2)
  1122.     def entryconfig(self, index, cnf={}, **kw):
  1123.         apply(self.tk.call, (self._w, 'entryconfigure', index)
  1124.               + self._options(cnf, kw))
  1125.     def index(self, index):
  1126.         i = self.tk.call(self._w, 'index', index)
  1127.         if i == 'none': return None
  1128.         return self.tk.getint(i)
  1129.     def invoke(self, index):
  1130.         return self.tk.call(self._w, 'invoke', index)
  1131.     def post(self, x, y):
  1132.         self.tk.call(self._w, 'post', x, y)
  1133.     def unpost(self):
  1134.         self.tk.call(self._w, 'unpost')
  1135.     def yposition(self, index):
  1136.         return self.tk.getint(self.tk.call(
  1137.             self._w, 'yposition', index))
  1138.  
  1139. class Menubutton(Widget):
  1140.     def __init__(self, master=None, cnf={}, **kw):
  1141.         Widget.__init__(self, master, 'menubutton', cnf, kw)
  1142.  
  1143. class Message(Widget):
  1144.     def __init__(self, master=None, cnf={}, **kw):
  1145.         Widget.__init__(self, master, 'message', cnf, kw)
  1146.  
  1147. class Radiobutton(Widget):
  1148.     def __init__(self, master=None, cnf={}, **kw):
  1149.         Widget.__init__(self, master, 'radiobutton', cnf, kw)
  1150.     def deselect(self):
  1151.         self.tk.call(self._w, 'deselect')
  1152.     def flash(self):
  1153.         self.tk.call(self._w, 'flash')
  1154.     def invoke(self):
  1155.         self.tk.call(self._w, 'invoke')
  1156.     def select(self):
  1157.         self.tk.call(self._w, 'select')
  1158.  
  1159. class Scale(Widget):
  1160.     def __init__(self, master=None, cnf={}, **kw):
  1161.         Widget.__init__(self, master, 'scale', cnf, kw)
  1162.     def get(self):
  1163.         return self.tk.getint(self.tk.call(self._w, 'get'))
  1164.     def set(self, value):
  1165.         self.tk.call(self._w, 'set', value)
  1166.  
  1167. class Scrollbar(Widget):
  1168.     def __init__(self, master=None, cnf={}, **kw):
  1169.         Widget.__init__(self, master, 'scrollbar', cnf, kw)
  1170.     def get(self):
  1171.         return self._getints(self.tk.call(self._w, 'get'))
  1172.     def set(self, *args):
  1173.         apply(self.tk.call, (self._w, 'set')+args)
  1174.  
  1175. class Text(Widget):
  1176.     def __init__(self, master=None, cnf={}, **kw):
  1177.         Widget.__init__(self, master, 'text', cnf, kw)
  1178.         self.bind('<Delete>', self.bspace)
  1179.     def bspace(self, *args):
  1180.             self.delete('insert')
  1181.     def tk_textSelectTo(self, index):
  1182.         self.tk.call('tk_textSelectTo', self._w, index)
  1183.     def tk_textBackspace(self):
  1184.         self.tk.call('tk_textBackspace', self._w)
  1185.     def tk_textIndexCloser(self, a, b, c):
  1186.         self.tk.call('tk_textIndexCloser', self._w, a, b, c)
  1187.     def tk_textResetAnchor(self, index):
  1188.         self.tk.call('tk_textResetAnchor', self._w, index)
  1189.     def compare(self, index1, op, index2):
  1190.         return self.tk.getboolean(self.tk.call(
  1191.             self._w, 'compare', index1, op, index2))
  1192.     def debug(self, boolean=None):
  1193.         return self.tk.getboolean(self.tk.call(
  1194.             self._w, 'debug', boolean))
  1195.     def delete(self, index1, index2=None):
  1196.         self.tk.call(self._w, 'delete', index1, index2)
  1197.     def get(self, index1, index2=None):
  1198.         return self.tk.call(self._w, 'get', index1, index2)
  1199.     def index(self, index):
  1200.         return self.tk.call(self._w, 'index', index)
  1201.     def insert(self, index, chars, *args):
  1202.         apply(self.tk.call, (self._w, 'insert', index, chars)+args)
  1203.     def mark_names(self):
  1204.         return self.tk.splitlist(self.tk.call(
  1205.             self._w, 'mark', 'names'))
  1206.     def mark_set(self, markName, index):
  1207.         self.tk.call(self._w, 'mark', 'set', markName, index)
  1208.     def mark_unset(self, markNames):
  1209.         apply(self.tk.call, (self._w, 'mark', 'unset') + markNames)
  1210.     def scan_mark(self, y):
  1211.         self.tk.call(self._w, 'scan', 'mark', y)
  1212.     def scan_dragto(self, y):
  1213.         self.tk.call(self._w, 'scan', 'dragto', y)
  1214.     def search(self, pattern, index, stopindex=None,
  1215.            forwards=None, backwards=None, exact=None,
  1216.            regexp=None, nocase=None, count=None):
  1217.         args = [self._w, 'search']
  1218.         if forwards: args.append('-forwards')
  1219.         if backwards: args.append('-backwards')
  1220.         if exact: args.append('-exact')
  1221.         if regexp: args.append('-regexp')
  1222.         if nocase: args.append('-nocase')
  1223.         if count: args.append('-count'); args.append(count)
  1224.         if pattern[0] == '-': args.append('--')
  1225.         args.append(pattern)
  1226.         args.append(index)
  1227.         if stopindex: args.append(stopindex)
  1228.         return apply(self.tk.call, tuple(args))
  1229.     def tag_add(self, tagName, index1, index2=None):
  1230.         self.tk.call(
  1231.             self._w, 'tag', 'add', tagName, index1, index2)
  1232.     def tag_unbind(self, tagName, sequence):
  1233.         self.tk.call(self._w, 'tag', 'bind', tagName, sequence, '')
  1234.     def tag_bind(self, tagName, sequence, func, add=''):
  1235.         if add: add='+'
  1236.         name = self._register(func, self._substitute)
  1237.         self.tk.call(self._w, 'tag', 'bind', 
  1238.                  tagName, sequence, 
  1239.                  (add + name,) + self._subst_format)
  1240.     def tag_config(self, tagName, cnf={}, **kw):
  1241.         apply(self.tk.call, 
  1242.               (self._w, 'tag', 'configure', tagName)
  1243.               + self._options(cnf, kw))
  1244.     def tag_delete(self, *tagNames):
  1245.         apply(self.tk.call, (self._w, 'tag', 'delete') + tagNames)
  1246.     def tag_lower(self, tagName, belowThis=None):
  1247.         self.tk.call(self._w, 'tag', 'lower', tagName, belowThis)
  1248.     def tag_names(self, index=None):
  1249.         return self.tk.splitlist(
  1250.             self.tk.call(self._w, 'tag', 'names', index))
  1251.     def tag_nextrange(self, tagName, index1, index2=None):
  1252.         return self.tk.splitlist(self.tk.call(
  1253.             self._w, 'tag', 'nextrange', tagName, index1, index2))
  1254.     def tag_raise(self, tagName, aboveThis=None):
  1255.         self.tk.call(
  1256.             self._w, 'tag', 'raise', tagName, aboveThis)
  1257.     def tag_ranges(self, tagName):
  1258.         return self.tk.splitlist(self.tk.call(
  1259.             self._w, 'tag', 'ranges', tagName))
  1260.     def tag_remove(self, tagName, index1, index2=None):
  1261.         self.tk.call(
  1262.             self._w, 'tag', 'remove', tagName, index1, index2)
  1263.     def window_cget(self, index, option):
  1264.         return self.tk.call(self._w, 'window', 'cget', index, option)
  1265.     def window_config(self, index, cnf={}, **kw):
  1266.         apply(self.tk.call, 
  1267.               (self._w, 'window', 'configure', index)
  1268.               + self._options(cnf, kw))
  1269.     def window_create(self, index, cnf={}, **kw):
  1270.         apply(self.tk.call, 
  1271.               (self._w, 'window', 'create', index)
  1272.               + self._options(cnf, kw))
  1273.     def window_names(self):
  1274.         return self.tk.splitlist(
  1275.             self.tk.call(self._w, 'window', 'names'))
  1276.     def yview(self, *what):
  1277.         apply(self.tk.call, (self._w, 'yview')+what)
  1278.     def yview_pickplace(self, *what):
  1279.         apply(self.tk.call, (self._w, 'yview', '-pickplace')+what)
  1280.  
  1281. class OptionMenu(Widget):
  1282.     def __init__(self, master, variable, value, *values):
  1283.         self.widgetName = 'tk_optionMenu'
  1284.         Widget._setup(self, master, {})
  1285.         self.menuname = apply(
  1286.             self.tk.call,
  1287.             (self.widgetName, self._w, variable, value) + values)
  1288.  
  1289. class Image:
  1290.     def __init__(self, imgtype, name=None, cnf={}, **kw):
  1291.         self.name = None
  1292.         master = _default_root
  1293.         if not master: raise RuntimeError, 'Too early to create image'
  1294.         self.tk = master.tk
  1295.         if not name: name = `id(self)`
  1296.         if kw and cnf: cnf = _cnfmerge((cnf, kw))
  1297.         elif kw: cnf = kw
  1298.         options = ()
  1299.         for k, v in cnf.items():
  1300.             if type(v) in CallableTypes:
  1301.                 v = self._register(v)
  1302.             options = options + ('-'+k, v)
  1303.         apply(self.tk.call,
  1304.               ('image', 'create', imgtype, name,) + options)
  1305.         self.name = name
  1306.     def __str__(self): return self.name
  1307.     def __del__(self):
  1308.         if self.name:
  1309.             self.tk.call('image', 'delete', self.name)
  1310.     def __setitem__(self, key, value):
  1311.         self.tk.call(self.name, 'configure', '-'+key, value)
  1312.     def __getitem__(self, key):
  1313.         return self.tk.call(self.name, 'configure', '-'+key)
  1314.     def height(self):
  1315.         return self.tk.getint(
  1316.             self.tk.call('image', 'height', self.name))
  1317.     def type(self):
  1318.         return self.tk.call('image', 'type', self.name)
  1319.     def width(self):
  1320.         return self.tk.getint(
  1321.             self.tk.call('image', 'width', self.name))
  1322.  
  1323. class PhotoImage(Image):
  1324.     def __init__(self, name=None, cnf={}, **kw):
  1325.         apply(Image.__init__, (self, 'photo', name, cnf), kw)
  1326.     def blank(self):
  1327.         self.tk.call(self.name, 'blank')
  1328.     def cget(self):
  1329.         return self.tk.call(self.name, 'cget')
  1330.     # XXX config
  1331.     # XXX copy
  1332.     def get(self, x, y):
  1333.         return self.tk.call(self.name, 'get', x, y)
  1334.     def put(self, data, to=None):
  1335.         args = (self.name, 'put', data)
  1336.         if to:
  1337.             args = args + to
  1338.         apply(self.tk.call, args)
  1339.     # XXX read
  1340.     # XXX write
  1341.  
  1342. class BitmapImage(Image):
  1343.     def __init__(self, name=None, cnf={}, **kw):
  1344.         apply(Image.__init__, (self, 'bitmap', name, cnf), kw)
  1345.  
  1346. def image_names(): return _default_root.tk.call('image', 'names')
  1347. def image_types(): return _default_root.tk.call('image', 'types')
  1348.  
  1349. ######################################################################
  1350. # Extensions:
  1351.  
  1352. class Studbutton(Button):
  1353.     def __init__(self, master=None, cnf={}, **kw):
  1354.         Widget.__init__(self, master, 'studbutton', cnf, kw)
  1355.         self.bind('<Any-Enter>',       self.tkButtonEnter)
  1356.         self.bind('<Any-Leave>',       self.tkButtonLeave)
  1357.         self.bind('<1>',               self.tkButtonDown)
  1358.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  1359.  
  1360. class Tributton(Button):
  1361.     def __init__(self, master=None, cnf={}, **kw):
  1362.         Widget.__init__(self, master, 'tributton', cnf, kw)
  1363.         self.bind('<Any-Enter>',       self.tkButtonEnter)
  1364.         self.bind('<Any-Leave>',       self.tkButtonLeave)
  1365.         self.bind('<1>',               self.tkButtonDown)
  1366.         self.bind('<ButtonRelease-1>', self.tkButtonUp)
  1367.         self['fg']               = self['bg']
  1368.         self['activebackground'] = self['bg']
  1369.